package com.service.impl;


import com.dao.StockDao;
import com.dao.impl.StockDaoImpl;
import com.entity.Stock;
import com.exception.CustomerException;
import com.result.QueryResult;
import com.service.StockService;
import com.util.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 服装服务接口实现类
 */
public class StockServiceImpl implements StockService {
    private StockDao stockDao=new StockDaoImpl();

    @Override
    public Stock add(Stock stock){
        try{
            Integer insert=stockDao.insert(stock);
            String lastInsertId = "select LAST_INSERT_ID()";
            PreparedStatement preparedStatement = DBUtil.createPreparedStatement(lastInsertId);
            ResultSet resultSet = DBUtil.execQuery(preparedStatement);

            if(resultSet.next()){
                stock.setId(resultSet.getInt(1));
            }
            return stock;
        }catch (Exception e){
            e.printStackTrace();
            throw new CustomerException("");
        }

    }

    @Override
    public void update(Stock stock) {
        stockDao.update(stock);
    }

    @Override
    public void upper(Integer id, Boolean inUsed) {
        stockDao.upper(id,inUsed);
    }

    @Override
    public void delete(Integer id) {
        stockDao.delete(id);
    }

    @Override
    public QueryResult selectByPage(Integer offset, Integer size) {
        return stockDao.selectByPage(offset,size);
    }
}
